package aliyunsms.sms.service;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author lmx
 * @Date 2021/4/26 23:25
 * @Version 1.0
 */
@Service
public class SMSImpl implements SMS{
    @Override
    public boolean send(String PhoneNum, String MdCode, Map<String, Object> map) {
        //连接阿里云
        DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", "LTAI5tPdBw5zfTcta4VyChim", "Ce26E1QFIqnCzVJKaQrLY0QyyaVTKe");
        IAcsClient client = new DefaultAcsClient(profile);

        //构建请求
        CommonRequest request = new CommonRequest();

        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        //自定义参数
        request.putQueryParameter("PhoneNumber",PhoneNum);//填手机号
        request.putQueryParameter("SignName","");//填签名的名称
        request.putQueryParameter("TemplateCode",MdCode);//填模板后的那代码
        //构建一个短信验证码

        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return  false;
    }
}
